package util;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
	public static int currentRoomID = -1;
	public static final String SESSION_KEY = "username";
	private static final String LEAGUE_MODE = "league";
	private static final String LEAGUE_MODE_ID = "league-id";
	
	private static final String USERNAME_PATTERN = "(([a-z, 1-9]).{6,60})";
	private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{5,32})"; // The password should contain the digit and upper character

	private static Pattern pattern;
	private static Matcher matcher;
	
	public static HashMap<String, String> SESSION = new HashMap<String, String>();
	
	
	public static boolean validUser(String username, String password) {
		boolean validUserName, validPassword;
		
		pattern = Pattern.compile(USERNAME_PATTERN);
		matcher = pattern.matcher(username);
		validUserName = matcher.matches();
		
		pattern = Pattern.compile(PASSWORD_PATTERN);
		matcher = pattern.matcher(password);
		validPassword = matcher.matches();
		
		return (validUserName & validPassword);
	}
	
	public static boolean validUsername(String username) {
		boolean validUserName;
		
		pattern = Pattern.compile(USERNAME_PATTERN);
		matcher = pattern.matcher(username);
		validUserName = matcher.matches();
	
		return validUserName;
	}
	
	public static boolean validPassword(String password) {
		boolean validPassword;
		
		pattern = Pattern.compile(PASSWORD_PATTERN);
		matcher = pattern.matcher(password);
		validPassword = matcher.matches();
		
		return validPassword;
	}
	
	public static void saveUserSession(String username) {
		SESSION.put(Utils.SESSION_KEY, username);
	}
	
	public static void removeUserSession() {
		SESSION.remove(Utils.SESSION_KEY);
	}
	
	public static Date getCurrentTime() {
		return new Date(System.currentTimeMillis());
	}
	
	public static boolean checkLoginUser() {
		
		return getCurrentUser() != null;
	}
	
	public static String getCurrentUser() {
		String username =  SESSION.get(Utils.SESSION_KEY);
		return  username;
	}
	
	public static Date getDate(String day, String month, String year) {
		//string containing date
        String strDate = "%s-%s-%s 00:00:00";
        strDate = String.format(strDate, year, month, day);

        //first convert string to java.util.Date object using SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        java.util.Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        return new Date(date.getTime());
	}

	public static boolean hasValue(String value) {
		return value != null && value.compareTo("") != 0;
	}

	public static void enterLeagueMode() {
		SESSION.put(LEAGUE_MODE, getCurrentUser());
	}

	public static void saveLeagueId(int leagueId) {
		SESSION.put(LEAGUE_MODE_ID, String.valueOf(leagueId));
	}

	public static int getLeagueId() {
		if(SESSION.get(LEAGUE_MODE_ID) == null)
			return -1;
		return Integer.valueOf(SESSION.get(LEAGUE_MODE_ID));
	}
	public static void leaveLeagueMode(){
		SESSION.remove(Utils.LEAGUE_MODE);
	}
	public static boolean isLeagueMode() {
		//return SESSION.get(LEAGUE_MODE).compareTo(getCurrentUser()) == 0;
		if(SESSION.get(LEAGUE_MODE)!= null)
			return true;
		return false;
	}
	
	public static boolean isThisDateValid(String dateToValidate, String dateFormat){ 
		if(dateToValidate == null || dateToValidate.equals("")){
			return false;
		}
 
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		sdf.setLenient(false);
		try {
			sdf.parse(dateToValidate);
		} catch (ParseException e){
			return false;
		}
 
		return true;
	}
	
	public static int compareDates(String strDate1, String strDate2, String dateFormat) throws ParseException{ 
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date1 = sdf.parse(strDate1);
    	Date date2 = sdf.parse(strDate2);
    	return date1.compareTo(date2);
	}
}
